import {promptBox, base, toast} from 'nc-lightapp-front';

export default class MainAction {

    constructor(comp) {
        this.comp = comp;
    }

    getClientHeight = () => {
        let {width} = document.querySelector('#app').getBoundingClientRect()
        let height = document.documentElement.clientHeight - 200;
        width = width - 256;
        this.comp.props.dispatch({
            type: 'myLeave/update',
            payload: {
                clientHeight: height,
                clientWidth: width
            }
        })
    }

    getTempalte = () => {
        const {dispatch} = this.comp.props;
        dispatch({
            type: 'myLeave/languageCreateUIDOM',
            payload: {
                ...this.comp.props,
                callback: this.initailMeta
            }
        })
    }
    getLangCode = (sName) => {
        var sRE = "(?:; )?" + sName + "=([^;]*);?";
        var oRE = new RegExp(sRE);
        if (oRE.test(document.cookie)) {
            return RegExp["$1"];
        }
        return null;
    }

    addOperateCol = (template, buttons) => {
        const {myLeave, button} = this.comp.props;
        let langCode = this.getLangCode('langCode');
        let oprWidth = '150px'
        if (langCode === 'english') {
            oprWidth = '240px'
        }
        let render1 = (text, record, index) => {
            let listBtns = []
            buttons.forEach(btn => {
                if (btn.area === 'list') {
                    listBtns.push(btn.key)
                }
            })

            return (
                <div className="currency-opr-col">
                    {button.createOprationButton(listBtns,
                        {
                            area: 'list',
                            buttonLimit: 2,
                            onButtonClick: (props, id, e) => {
                                this.listClickHandle(id, record)
                            }
                        }
                    )}
                </div>
            );
        }
        template['outside_list'].items.push({
            attrcode: 'operation',
            renderStatus: 'browse',
            itemtype: 'customer',
            label: myLeave.json['6040-000019'], /* 国际化处理： 操作*/
            width: oprWidth,
            visible: true,
            fixed: 'right',
            render: render1
        })
    }

    listClickHandle = (key, record) => {
        if (key === 'list_flow') {
            this.showFlowHistory(record, 1)
        } else if (key === 'list_file') {
            this.showUploader(record);
        }
    }

    showUploader = (record) => {
        const {dispatch} = this.comp.props;
        dispatch({
            type: 'myLeave/update',
            payload: {
                showUploader: true,
                billId: record.values['pk_outside'].value,
                approvestatus: record.values['approvestatus'].value,
            }
        })
    }

    initailMeta = (metas, lang, intl) => {
        let {button, template} = metas;
        const props = this.comp.props;
        const {renderItem, meta, dispatch} = props;
        dispatch({
            type: 'myLeave/update',
            payload: {
                json: lang
            }
        })
        this.addOperateCol(template, button);
        meta.setMeta(template ? template : {});
        props.button.setButtons(button);
        dispatch({
            type: 'myLeave/update',
            payload: {
                templateInited: true
            }
        })
    }

    /**
     * @desc:
     * @param {type}
     * @return:
     */
    showFlowHistory = (record, type) => {
        const {dispatch, myLeave} = this.comp.props;
        let billId
        let billtype
        billtype = record.values['billtype'].value
        billId = record.values['pk_' + myLeave.activeTab].value;
        billtype = (record.values['transtype'] && record.values['transtype'].value) || billtype
        dispatch({
            type: 'myLeave/update',
            payload: {
                showApproveDetail: true,
                billId,
                billtype
            }
        })
    }

    /**
     * @desc: 单据撤回操作
     * @param {type}
     * @return:
     */
    recallBill = (record) => {
        const {dispatch, myLeave, editTable} = this.comp.props;
        let pk = record.values['pk_' + myLeave.activeTab].value;
        let tableCode = myLeave.activeTab + '_list';
        let activeTab = myLeave.activeTab
        let status = record.values.approvestatus.value;
        let revokestatus = record.values.approvestatus.value === '1' && (record.values.tripoffapprovestatus && record.values.tripoffapprovestatus.value || record.values.leaveoffapprovestatus && record.values.leaveoffapprovestatus.value)
        if (status === '1' && revokestatus === '3') {
            pk = record.values['pk_' + myLeave.activeTab + 'off'].value;
            activeTab = myLeave.activeTab + 'off'
        }
        promptBox({
            color: "warning",
            title: myLeave.json['6040-00005'], /* 国际化处理： 提示*/
            content: myLeave.json['6040-000023'], /* 国际化处理： 你确定要撤回该单据吗？*/
            beSureBtnClick: () => {
                dispatch({
                    type: 'myLeave/recallAction',
                    payload: {
                        key: activeTab,
                        pk
                    }
                }).then(res => {
                    if (res.success) {
                        toast({
                            color: 'success',
                            content: myLeave.json['6040-000024']
                        });
                        dispatch({
                            type: 'myLeave/queryListAction',
                            payload: {
                                key: myLeave.activeTab,
                                beginDate: myLeave.beginValue,
                                endDate: myLeave.endValue
                            }
                        }).then(res => {
                            if (res.success) {
                                if (res.data) {
                                    editTable.setTableData(tableCode, res.data[tableCode])
                                    dispatch({
                                        type: 'myLeave/update',
                                        payload: {
                                            [myLeave.activeTab + 'PageInfo']: res.data[tableCode].pageInfo
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            }
        })
    }
    closeApproveDetailDialog = () => {
        const {dispatch} = this.comp.props;
        dispatch({
            type: 'myLeave/update',
            payload: {
                showApproveDetail: false
            }
        })
    }
    handlePageIndexChange = (pageIndex) => {
        const {dispatch, myLeave} = this.comp.props
        let pageInfo = myLeave.activeTab + 'PageInfo'
        dispatch({
            type: 'myLeave/update',
            payload: {
                [pageInfo]: Object.assign({}, myLeave[pageInfo], {
                    pageIndex
                })
            }
        })
        this.queryListAction()
    }
    didMount = () => {
        if (window.location.href.match(/(localhost|127\.0\.0\.1)/)) {
            window.location.hash = `#ifr?page=2019916105952100`;
        }
        this.getClientHeight()
        this.getTempalte()
    }
}